package com.ph.permission.api.controller;


import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.shopping.facade.permission.dto.UpdatePassDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.RoleEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.permission.dto.UpdateUserRoleDto;
import com.ph.shopping.facade.permission.dto.UserDto;
import com.ph.shopping.facade.permission.dto.UserRoleDto;
import com.ph.shopping.facade.permission.entity.User;
import com.ph.shopping.facade.permission.service.IUserService;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping(value="api/user")
public class UserController extends BaseController {

	private final static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Reference(version="1.0.0")
	private IUserService userService;
	
	/**
	 * 获取用户列表
	 * @param page
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/list",method= RequestMethod.GET)
	@ResponseBody
	public Object list(@ModelAttribute PageBean page,@ModelAttribute UserDto userDto){
		Result result=userService.getUserList(page, userDto);
		return result;	
	}
	
	/**
	 * 新增用户
	 * @param userDto
	 * @return
	 */
	@RequestMapping(value="/add",method= RequestMethod.POST)
	@ResponseBody
	public Object add(@ModelAttribute UserRoleDto userDto){
		userDto.setRoleCode(RoleEnum.ADMIN.getCode());
		return userService.addUser(userDto);
	}
	
	/**
	 * 删除账户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/delete",method= RequestMethod.POST)
	@ResponseBody
	public Object delete(@ModelAttribute User user){
		return userService.deleteUser(user);
	}
	
	
	/**
	 * 重置密码
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/resetPass",method= RequestMethod.POST)
	@ResponseBody
	public Object resetPass(Long userId){
		User user = new User();
		user.setId(userId);
		return userService.resetPassword(user);
	}
	
	/**
	 * 冻结账户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/frozen",method= RequestMethod.POST)
	@ResponseBody
	public Object frozenCount(@ModelAttribute User user){
		user.setIsable(new Byte("2"));
		return userService.frozenOrEnableUser(user);
	}
	
	/**
	 * 启用账户
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/start",method= RequestMethod.POST)
	@ResponseBody
	public Object startCount(@ModelAttribute User user){
		user.setIsable(new Byte("1"));
		return userService.frozenOrEnableUser(user);
	}
	
	/**
	 * 查询账户角色
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getUserRole",method= RequestMethod.GET)
	@ResponseBody
	public Object delete(Long userId){
		return userService.queryUserRoleByUserID(userId);
	}
	/**
	 * 分配角色
	 * @param ud
	 * @return
	 */
	@RequestMapping(value="/updateUserPermission",method= RequestMethod.POST)
	@ResponseBody
	public Object delete(@ModelAttribute UpdateUserRoleDto ud){
		//判断角色是否选择
		if (ud.getRoleIDs()==null||ud.getRoleIDs().size()==0) {
			return new Result(ResultEnum.ROLE_NOT_NULL_ERROR.getCode(), ResultEnum.ROLE_NOT_NULL_ERROR.getMsg(), "[]", 0);
		}
		return userService.updateUserRole(ud);
	}
	
	/**
	 * 后台用户修改密码
	 * @return
	 */
	@RequestMapping(value="/updatePsw",method= RequestMethod.POST)
	@ResponseBody
	public Object updatePsw(UpdatePassDto updatePassDto, HttpServletRequest request){
		Result result = getResult(ResultEnum.FAIL);
		try {
			logger.info("修改密码入参，updatePassDto={}", JSON.toJSONString(updatePassDto));
			if (null != updatePassDto) {
				updatePassDto.setId(getLoginUser(request).getId());
				result = userService.updatePass(updatePassDto);
			}
			logger.info("修改密码返回值，result={}", JSON.toJSONString(result));
		} catch (Exception e) {
			logger.info("修改密码异常，e={}", e);
		}
		return result;
	}
	/**
	 * 跳转列表页面
	 * @return
	 */
	@RequestMapping(value="/listPage",method=RequestMethod.GET)
	public String listRolePage(){
		return "/permission/user/list";
	}
	
	/**
	 * 跳转新增页面
	 * @return
	 */
	@RequestMapping(value="/addUserPage",method=RequestMethod.GET)
	public String addRolePage(){
		return "/permission/user/add";
	}
	
	/**
	 * 跳转修改页面
	 * @return
	 */
	@RequestMapping(value="/updatePage",method=RequestMethod.GET)
	public ModelAndView updateRolePage(@ModelAttribute User user){
		ModelAndView mv=new ModelAndView("/permission/user/update");
		Result result=userService.queryUser(user);
		mv.addObject("user", result.getData());
		return mv;
	}


	/**
	 * 跳转修改页面
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(@ModelAttribute User user){
		return userService.updateUser(user);
	}
}
